﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchwandoujia.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");

//coolchuan2012
$link = mysql_connect("localhost","root",DBPAS); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('TYPEURLFOOTER','&os=17&model=HTC+Sensation&cpu=pyramid&order=download');
define('ONCECATCHCOUNT',20);

define('ONCECATCHCOUNTMAX',40);

define('WEBURLHEADER','http://www.wandoujia.com/apps/');

define('LOGPATHHEADER','/var/www/catchjiankongdata/');
//define('LOGPATHHEADER','');

define('LOGFORSUCCESS',0);


define('WAPURLHEADERWANDOUJIA','http://apps.wandoujia.com/api/v1/apps/');
define('WAPURLFOOTERWANDOUJIA','?f=phoenix2&v=3.34.1&u=ecaf666a0f8e4553b843d5e61e80c62ad30247cd&max=20&start=0&opt_fields=apks.compatible%2Capks.incompatibleDetail%2Cad%2Capks.adsType%2Capks.beta%2Capks.bytes%2Capks.downloadUrl.*%2Capks.size%2Capks.versionCode%2Capks.versionName%2Capks.securityStatus%2Capks.md5%2Capks.permissionLevel%2Capks.superior%2Capks.dangerousPermissions%2Capks.permissions%2Capks.securityDetail.*%2Capks.resolution%2Ccategories.alias%2Ccategories.name%2Cdescription%2Ctitle%2CcommentsCount%2ClikeCount%2CdislikesCount%2CinstalledCountStr%2Cicons.px78%2CpackageName%2Cid%2ClikesRate%2Cscreenshots.*%2Capks.verified%2Capks.paidType%2CimprUrl%2Cbanner%2Ctags.*&vc=4197&ch=wandoujia_wap&pos=m/tag/shopping');

catchdowntimes();

function getdownloadtimes($packagename)
{
	$downloadtime = 0;
	$downloadtimesurl = "http://index.wandoujia.com/muce-index/apps/statistics/downloads/$packagename?days=3";
	@$json_string = file_get_html($downloadtimesurl);
	$obj = json_decode($json_string);
	$downloadtimearr = $obj->data;
	if(count($downloadtimearr) == 0)
	{
		$json_string = "";
		return $downloadtime;
	}
	
	foreach ($downloadtimearr as $downloadtimeoneday)
	{
		$todaydate = strtotime(date("Ymd"));
		if($downloadtimeoneday->date == date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y"))))
		{
			$downloadtime = $downloadtimeoneday->downloads;
			$json_string = "";
			return $downloadtime;
		}
	}
	$json_string = "";
	return $downloadtime;
}


function get_exit_packagename_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id, packageName,title from jk_apps_info_wandoujia where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_packagename = array();
  	while($row=mysql_fetch_array($rs))        
  	{
	  	$app_exit_packagename[] = $row['packageName'];
  	}
	return $app_exit_packagename;
}

function get_exit_appinfo_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id, packageName,title,apptype,appbigtype from jk_apps_info_wandoujia where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['AppPackageName'] = $row['packageName'];
	  	$app_exit_info_temp['AppName'] = $row['title'];
	  	$app_exit_info_temp['apptype'] = $row['apptype'];
	  	$app_exit_info_temp['appbigtype'] = $row['appbigtype'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}

function get_exit_appversion_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id,versionName,versionCode from jk_apps_version_wandoujia where IsDeleted=0 order by id asc '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_version = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_version_temp = array();
	  	$app_exit_version_temp['id'] = $row['id'];
	  	$app_exit_version_temp['app_id'] = $row['app_id'];
	  	$app_exit_version_temp['versionName'] = $row['versionName'];
	  	$app_exit_version_temp['versionCode'] = $row['versionCode'];
	  	$app_exit_version[] = $app_exit_version_temp;
  	}
	return $app_exit_version;
}



function get_today_score()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$today = date("Y-m-d");

	$sql = 'select id, app_id from jk_apps_score_data_mobile_wandoujia_'.date('Ym',time()).' where IsDeleted=0 and downtimestoday =0 and time="'.$today.'"'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
	  	$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}


function catchdowntimes()
{
	$app_today_score = get_today_score();
	
	$app_exit_infos = get_exit_appinfo_fromdb();
	foreach ($app_today_score as $app_today_score_data)
	{
		$packagename = "";
		foreach ($app_exit_infos as $app_exit_info)
		{
			if($app_exit_info['id'] == $app_today_score_data['app_id'])
			{
				$packagename = $app_exit_info['AppPackageName'];
				break;
			}
		}
		
		if($packagename == "")
		{
			continue;
		}
		$appDownTimesToday = getdownloadtimes($packagename);
		
		if(($appDownTimesToday == 0) || ($appDownTimesToday == ""))
		{
			continue;
		}
		
		update_downtimes_todb($app_today_score_data['id'],$appDownTimesToday);
	}
}

function update_downtimes_todb($appdataid,$appDownTimesToday)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	
	if(($appdataid == 0) || ($appDownTimesToday == 0))
	{
		return ;
	}
	
	$sql = 'update jk_apps_score_data_mobile_wandoujia_'.date('Ym',time()).' set downtimestoday = '.$appDownTimesToday.' where id='. $appdataid. '';
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/wandoujia_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_db_fail.log';
	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}


?>
